import {
  Body,
  Controller,
  Get,
  Param,
  Headers,
  Query,
  Post,
  Put,
  Delete,
  UseInterceptors,
  ClassSerializerInterceptor,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { IsAuth } from 'src/decorators/auth.decorator';
import { IResponseData, responseData } from 'src/utils/result';
import { DepartmentService } from './department.service';
import {
  CreateDepartmentDto,
  DepartmentDto,
  UpdateDepartmentDto,
} from './department.dto';
import { handleTree } from 'src/utils/tools';
@ApiTags('部门模块')
@ApiBearerAuth()
@Controller('department')
export class DepartmentController {
  constructor(private readonly departmentService: DepartmentService) {}
  @ApiOperation({ summary: '查询部门' })
  @ApiResponse({
    status: 200,
    description: '查询部门',
    type: DepartmentDto,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get()
  @IsAuth('system:dept:list')
  async getAll(@Headers() headers): Promise<IResponseData> {
    const res = await this.departmentService.getAll({
      where: {
        is_delete: '0',
      },
      order: {
        sort: 'ASC',
      },
    });
    return responseData({
      code: 100010,
      data: res,
      language: headers['language'],
    });
  }

  @ApiOperation({ summary: '查询部门详情' })
  @ApiResponse({
    status: 200,
    description: '查询部门详情',
    type: DepartmentDto,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get(':id')
  @IsAuth('system:dept:details')
  async find(
    @Param('id') id: number,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.departmentService.getOne({
      where: {
        id,
      },
    });
    return responseData({
      code: 100010,
      data: res,
      language: headers['language'],
    });
  }

  @Post()
  @IsAuth('system:dept:add')
  @ApiOperation({ summary: '创建部门' })
  @ApiResponse({ status: 200, description: '操作成功' })
  async create(
    @Body() dto: CreateDepartmentDto,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.departmentService.create(dto);
    return responseData({
      code: 100020,
      data: res,
      language: headers['language'],
    });
  }

  @Put()
  @IsAuth('system:dept:update')
  @ApiOperation({ summary: '更新部门' })
  @ApiResponse({ status: 200, description: '操作成功' })
  async update(
    @Body() dto: UpdateDepartmentDto,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.departmentService.update(dto);
    return responseData({
      code: 100030,
      data: res,
      language: headers['language'],
    });
  }

  @Delete()
  @IsAuth('system:dept:delete')
  @ApiOperation({ summary: '删除部门' })
  @ApiResponse({ status: 200, description: '操作成功' })
  async delete(
    @Query('ids') query: string,
    @Headers() headers,
  ): Promise<IResponseData> {
    const { lists } = await this.departmentService.getAll();
    const data = handleTree(lists);
    const ids = query.split(',');
    for (let i = 0; i < ids.length; i++) {
      if (data.obj[ids[i]].children.length) {
        throw new Error('含有子集，请先删除子集');
      }
    }
    const res = await this.departmentService.update_delete(query);
    return responseData({
      code: 100040,
      data: res,
      language: headers['language'],
    });
  }
}
